package com.crm.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.dao.BizSysuserCitysMapper;
import com.crm.model.BizSysuserCitys;
import com.crm.model.SysUserCitys;
import com.crm.model.SysUserCitysModel;
import com.crm.service.BizSysuserCitysService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class BizSysuserCitysServiceImpl extends BaseServiceImpl<BizSysuserCitys> implements BizSysuserCitysService{

	@Autowired
	private BizSysuserCitysMapper bizSysuserCitysMapper;
	
	@Override
	public List<BizSysuserCitys> selectAllCU(Page<BizSysuserCitys> page, BizSysuserCitys Record) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize(),"t.create_date desc");
		List<BizSysuserCitys> list = bizSysuserCitysMapper.selectAllByWhere(Record);
		return list;
	}

	@Override
	public List<BizSysuserCitys> selectAllByDepid(Page<BizSysuserCitys> page, BizSysuserCitys t) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), "t.create_date desc");
		
		List<BizSysuserCitys> list = bizSysuserCitysMapper.selectAllByDepid(t.getDepartmentId());
		
		return list;
	}

	@Override
	public List<BizSysuserCitys> selectByDepartment(String departmentId) {
		List<BizSysuserCitys> list = bizSysuserCitysMapper.selectByDepartment(departmentId);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectByGroup(Page<BizSysuserCitys> page, BizSysuserCitys record) {
		
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), "m.code asc");
		
		List<Map<String, Object>> list = bizSysuserCitysMapper.selectByGroup(record);
		
		return list;
	}

	@Transactional
	@Override
	public void updateBatch(SysUserCitysModel sysUserCitys, String departmentId, String echelonId) {
		
		//删除当前部门所以创建的人员城市匹配信息
		bizSysuserCitysMapper.deleteByDepartmentId(departmentId);
		
		//循环插入人员城市匹配表
		List<SysUserCitys> list = sysUserCitys.getSysUserCitys();
		for(SysUserCitys s : list){
			String sysUserId = s.getSysUserId();
			
			String citysId = s.getCitysId();
			BizSysuserCitys record =new BizSysuserCitys();
			record.setSysuserId(sysUserId);
			record.setCitysId(citysId);
			record.setDepartmentId(departmentId);
			record.setEchelonId(echelonId);
			record.setCreateDate(new Date());
			
			bizSysuserCitysMapper.insert(record);
		}
		
	}

}
